package com.goushu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goushu.mapper.BookMapper;
import com.goushu.pojo.Book;
import com.goushu.pojo.Comment;
import com.goushu.pojo.PageBean;
import com.goushu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> listMain() {
        return bookMapper.listMain();
    }

    @Override
    public Book listDetail(Integer id) {
        return bookMapper.listDetail(id);
    }

    @Override
    public List<Comment> listComment(Integer id) {
        return bookMapper.listComment(id);
    }

    @Override
    public PageBean<Book> search(Integer pageNum, Integer pageSize, String keyword) {
        PageBean<Book> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Book> bl = bookMapper.search(keyword);
        Page<Book> p = (Page<Book>) bl;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public PageBean<Book> category(Integer pageNum, Integer pageSize, String keyword) {
        PageBean<Book> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Book> bl = bookMapper.category(keyword);
        Page<Book> p = (Page<Book>) bl;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}
